package com.ruoyi.web.controller.order;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WaterWorkOrderNew;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.IWaterWorkOrderNewService;
import com.ruoyi.system.service.IWaterWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工作工单Controller
 * 
 * @author ruoyi
 * @date 2025-09-29
 *  * 此Controller为第二个版本的工单
 */
@RestController
@RequestMapping("/service/erpWork")
public class WaterWorkOrderNewController extends BaseController
{
    @Autowired
    private IWaterWorkOrderNewService waterWorkOrderNewService;

    @Autowired
    private IWaterWorkOrderService waterWorkOrderService;

    @Autowired
    private ISysDeptService selectDeptService;

    /**
     * 查询工作工单列表
     */
    @PreAuthorize("@ss.hasPermi('service:erpWork:list')")
    @GetMapping("/list")
    public TableDataInfo list(WaterWorkOrderNew waterWorkOrderNew)
    {
        startPage();
        List<WaterWorkOrderNew> list = waterWorkOrderNewService.selectWaterWorkOrderNewList(waterWorkOrderNew);
        return getDataTable(list);
    }

    /**
     * 导出工作工单列表
     */
    @PreAuthorize("@ss.hasPermi('service:erpWork:export')")
    @Log(title = "工作工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WaterWorkOrderNew waterWorkOrderNew)
    {
        List<WaterWorkOrderNew> list = waterWorkOrderNewService.selectWaterWorkOrderNewList(waterWorkOrderNew);
        ExcelUtil<WaterWorkOrderNew> util = new ExcelUtil<WaterWorkOrderNew>(WaterWorkOrderNew.class);
        util.exportExcel(response, list, "工作工单数据");
    }

    /**
     * 获取工作工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:erpWork:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(waterWorkOrderNewService.selectWaterWorkOrderNewById(id));
    }

    /**
     * 新增工作工单
     */
    @PreAuthorize("@ss.hasPermi('service:erpWork:add')")
    @Log(title = "工作工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WaterWorkOrderNew waterWorkOrderNew)
    {
        waterWorkOrderNew.setStatue("1");
        waterWorkOrderNew.setOrderId(waterWorkOrderService.getMaxOrderNumber());
        waterWorkOrderNew.setStartDeptId(getDeptId()+"");
        waterWorkOrderNew.setStartUserId(getUserId()+"");
        waterWorkOrderNew.setStartUser(getUsername());
        waterWorkOrderNew.setStartDept(selectDeptService.selectDeptById(Long.valueOf(waterWorkOrderNew.getStartDeptId())).getDeptName());
        return toAjax(waterWorkOrderNewService.insertWaterWorkOrderNew(waterWorkOrderNew));
    }

    /**
     * 修改工作工单
     */
    @PreAuthorize("@ss.hasPermi('service:erpWork:edit')")
    @Log(title = "工作工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WaterWorkOrderNew waterWorkOrderNew)
    {
        return toAjax(waterWorkOrderNewService.updateWaterWorkOrderNew(waterWorkOrderNew));
    }

    /**
     * 删除工作工单
     */
    @PreAuthorize("@ss.hasPermi('service:erpWork:remove')")
    @Log(title = "工作工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(waterWorkOrderNewService.deleteWaterWorkOrderNewByIds(ids));
    }
}
